<?php

class ManageGallery extends CI_Controller {

    private $user;

    function ManageGallery() {
        parent::__construct();
        $id = $this->session->userdata('id');
        $adminname = $this->session->userdata('adminname');
        $hash = $this->session->userdata('hash');
        if (!empty($hash) && $hash == md5(SECRETCODE . $id . $adminname . SECRETCODE)) {
            $this->admin['name'] = $adminname;
            $this->admin['id'] = $id;
            $this->admin['hash'] = $hash;
            $this->load->model('gallery_m');
        } else {
            redirect(base_url() . 'admin/login');
        }
    }

    function index() {
        if ($this->admin['name'] != '' && $this->admin['id'] != '' && $this->admin['hash'] == md5(SECRETCODE . $this->admin['id'] . $this->admin['name'] . SECRETCODE)) {
            $this->load->model('Gallery_m', 'gallery');
            $this->load->model('images_m', 'photo');

            $data['galleries'] = $this->gallery->all_gallery();
            foreach ($data['galleries'] as &$gal) {
                $photos = $this->photo->get_by_gallery($gal->id);
                if (count($photos) != $gal->photo) {
                    $gal->photo = count($photos);
                    $this->db->where('id', $gal->id);
                    $this->db->update('gallery', array('photo' => count($photos)));
                }
            }

            $this->load->view('admin/common/header', $this->user);
            $this->load->view('admin/gallery', $data);
            $this->load->view('admin/common/footer');
        } else
            redirect(base_url() . 'admin/login');
    }

    function add() {
        if ($this->admin['name'] != '' && $this->admin['id'] != '' && $this->admin['hash'] == md5(SECRETCODE . $this->admin['id'] . $this->admin['name'] . SECRETCODE)) {
            if ($this->input->post('add')) {
                $data['name'] = $this->input->post('name');
                $data['description'] = $this->input->post('desc');
		$data['show_top'] = $this->input->post('show_top');
		$data['show_slide'] = $this->input->post('show_slide');
		
                //$data['publish'] = $this->input->post('publish');
                if (is_array($this->input->post('images')))
                    $data['photo'] = count($this->input->post('images'));

                $images = $this->input->post('images');
                $imagedess = $this->input->post('imagedess');
                $imagenames = $this->input->post('imagenames');
                $imagetitles = $this->input->post('imagetitles');
                $imageprices = $this->input->post('imageprices');
		
                if (!$this->db->insert('gallery', $data))
                    die("Can't insert to database!");
                $galid = $this->db->insert_id();
                $avatar = 0;
                if (is_array($images)) {
                    for ($i = 0; $i < count($images); $i++) {
                        $data = array();
                        $data['name'] = $imagenames[$i];
                        $data['path'] = 'uploads/' . $images[$i];
                        //$data['title'] = $imagetitles[$i];
                        $data['gallery_id'] = $galid;
                        $demension = getimagesize('uploads/' . $images[$i]);
                        //$data['dimensions'] = $demension[0] . 'x' . $demension[1];
                        $data['description'] = $imagedess[$i];
                        $data['date_created'] = time();
                        $this->db->insert('images', $data);
                        if ($avatar == 0)
                            $avatar = $this->db->insert_id();
                    }
                }
                if ($avatar > 0) {
                    $data = array(
                        'avatar' => $avatar
                    );
                    $this->db->where('ID', $galid);
                    $this->db->update('gallery', $data);
                }
                echo 'success';
            } else {

                //die("Hieu");

                $this->load->library('ckeditor');
                $this->load->library('ckfinder');
                //die("Hieu");

                $data['editor'] = new CKEditor(base_url() . "js/ckeditor");
                $this->ckfinder->SetupCKEditor($data['editor'], base_url() . 'js/ckfinder');

                $data['PHPSESSID'] = $this->session->userdata('session_id');
                $data['info'] = 'Thêm mới gallery';
                $this->load->view('admin/common/header', $this->user);
                $this->load->view('admin/gallery_add', $data);
                $this->load->view('admin/common/footer');
            }
        } else
            redirect(base_url() . 'admin/login');
    }

    function update($id) {
        if ($this->admin['name'] != '' && $this->admin['id'] != '' && $this->admin['hash'] == md5(SECRETCODE . $this->admin['id'] . $this->admin['name'] . SECRETCODE)) {
            if ($this->input->post('update')) {
                $data['name'] = $this->input->post('name');
                $data['description'] = $this->input->post('desc');
                $data['show_top'] = $this->input->post('show_top');
		$data['show_slide'] = $this->input->post('show_slide');
		
		
		
                $imagesids = $this->input->post('imageids');
                $images = $this->input->post('images');
                $imagedess = $this->input->post('imagedess');
                $imagenames = $this->input->post('imagenames');
                $imagetitles = $this->input->post('imagetitles');
                $imageprices = $this->input->post('imageprices');

                $this->db->where('id', $id);
                $this->db->set('photo', 'photo + ' . count($images), FALSE);
                $this->db->update('gallery', $data);

                $tmp = "";
                //echo "MOI = " . count($images) . "</br>" . $images[0];

                $startindex = count($imagenames) - count($images);
                if (count($images) > 0) {
                    for ($i = 0; $i < count($images); $i++) {
                        if ($images[$i] == '') {
                            break;
                        }
                        $data = array();
                        $data['name'] = $imagenames[$startindex + $i];
                        //$data['title'] = $imagetitles[$startindex + $i];
                        $data['path'] = 'uploads/' . $images[$i];
                        $data['gallery_id'] = $id;
                        $dimension = @getimagesize('uploads/' . $images[$i]);
                        // $data['dimensions'] = $dimension[0] . 'x' . $dimension[1];
                        $data['description'] = $imagedess[$startindex + $i];

                        $data['date_created'] = time();
                        $this->db->insert('images', $data);
                    }
                }

                for ($i = 0; $i < count($imagenames); $i++) {

                    $data = array();
                    $tmp = $tmp . " " . $i . " " . $imagenames[$i] . " -- ";
                    $data['name'] = $imagenames[$i];
                    // $data['title'] = $imagetitles[$i];
                    $data['description'] = $imagedess[$i];
                    $data['gallery_id'] = $id;
                    $this->db->where('id', $imagesids[$i]);
                    $this->db->update('images', $data);
                }

                echo 'success';
            } else {
                $this->load->model('Gallery_m', 'gallery');
                $gallery = $this->gallery->get($id, 0, 0);
                if (!$gallery)
                    redirect(base_url() . 'admin/managegallery');

                $data['name'] = $gallery->name;
                $data['description'] = $gallery->description;
                $data['show_top'] = $gallery->show_top;
		$data['show_slide'] = $gallery->show_slide;
		
                $data['id'] = $id;

                $this->load->model('Images_m', 'photo');
                $data['photos'] = $this->photo->get_by_gallery($id);

                $data['info'] = 'Sửa gallery' . $gallery->name;

                $this->load->library('ckeditor');
                $data['editor'] = new CKEditor(base_url() . 'js/ckeditor/');

                $data['PHPSESSID'] = $this->session->userdata('session_id');

                $this->load->view('admin/common/header', $this->user);
                $this->load->view('admin/gallery_edit', $data);
                $this->load->view('admin/common/footer');
            }
        } else
            redirect(base_url() . 'admin/login');
    }

    function delete() {
        if ($this->admin['name'] != '' && $this->admin['id'] != '' && $this->admin['hash'] == md5(SECRETCODE . $this->admin['id'] . $this->admin['name'] . SECRETCODE)) {
            $ids = $this->input->post('selected');
            foreach ($ids as $id) {
                if (is_numeric($id)) {
                    $this->db->where('ID', $id);
                    if ($this->db->delete('gallery')) {
                        $this->db->where('gallery_id', $id);
                        $this->db->delete('images');
                    }
                }
            }
            redirect(base_url() . 'admin/managegallery');
        } else
            redirect(base_url() . 'admin/login');
    }

    function deletePhoto($id, $galid) {
        if ($this->admin['name'] != '' && $this->admin['id'] != '' && $this->admin['hash'] == md5(SECRETCODE . $this->admin['id'] . $this->admin['name'] . SECRETCODE)) {
            $this->db->where('id', $id);
            $this->db->delete('images');

            $this->db->where('ID', $galid);
            $this->db->set('photo', 'photo - 1', FALSE);
            $this->db->update('gallery');
        } else
            redirect(base_url() . 'admin/login');
    }

    function upload() {
        if ($this->admin['name'] != '' && $this->admin['id'] != '' && $this->admin['hash'] == md5(SECRETCODE . $this->admin['id'] . $this->admin['name'] . SECRETCODE)) {
            $config['upload_path'] = './uploads/';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '20000000'; //20M
            $config['overwrite'] = false;
            $this->load->library('upload', $config);
	    
            if (!$this->upload->do_upload('imageFile')) {
                echo "error|" . $this->upload->display_errors();
            } else {
                $respond = $this->upload->data();

                $imagesize = getimagesize($respond['full_path']);
                //Make thumbnail
                $image['source_image'] = $respond['full_path'];
                $image['create_thumb'] = true;
                $image['thumb_marker'] = '';
                $image['new_image'] = './uploads/thumbs/' . $respond['file_name'];
                $image['width'] = (int) (($imagesize[0] * 100) / $imagesize[1]);
                $image['height'] = 100;
                //$this->load->library('image_lib', $image);
                //if ($this->image_lib->resize()) {
                //    echo $respond['file_name'];
                //} else {
                //    echo 'error|' . $this->image_lib->display_errors();
                //}
                echo $respond['file_name'];
            }
        } else
            redirect(base_url() . 'admin/login');
    }

}